package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ofps.forms.common.utils.R;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.ofps.forms.mapper.AnswerBlankMapper;
import com.ofps.forms.domain.AnswerBlank;
import com.ofps.forms.service.AnswerBlankService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service
public class AnswerBlankServiceImpl extends ServiceImpl<AnswerBlankMapper, AnswerBlank> implements AnswerBlankService {

    @Resource
    private AnswerBlankMapper answerBlankMapper;

    @Override
    public R save(HttpServletRequest request, AnswerBlank answerBlank) {
        //设置userId
//        answerBlank.setUserId(TokenUtil.getId(request));
//        answerBlank.setUserId(Math.toIntExact(getUserId()));
        return this.saveOrUpdate(answerBlank) ? R.ok("保存成功！") : R.error("保存失败！");
    }

    @Override
    public R delete(Map<String, Object> body) {
        //根据id查询删除
        QueryWrapper<AnswerBlank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return answerBlankMapper.delete(queryWrapper) == 1 ? R.ok("删除成功！") : R.error("删除失败！");
    }

    @Override
    public List<AnswerBlank> queryAnsBlankWithQuestionId(String question_id) {
        //未测试
        //依据问题id查询
        QueryWrapper<AnswerBlank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", question_id);
        List<AnswerBlank> answerBlankList = answerBlankMapper.selectList(queryWrapper);
        return answerBlankList;
    }

    @Override
    public Map<String, Object> summerizeAnswerBlankTable(List<AnswerBlank> answerBlankEntityList) {
        //统计答案表中所有的信息
        //功能：统计一道填空题中所有的信息
        List<String> BlankMessages = new ArrayList<>();
        Integer sum = 0;
        Map<String,Object> summrizeMessages = new HashMap<>();
        Integer fillNum = 0;
        for (AnswerBlank answerBlankEntity : answerBlankEntityList) {
            BlankMessages.add(answerBlankEntity.getAnswer());
        }
        summrizeMessages.put("BlankAnsMessages",BlankMessages);
        summrizeMessages.put("Type",0);
        summrizeMessages.put("sum",answerBlankEntityList.size());
        return summrizeMessages;
    }
}
